﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;
using Common;

namespace DAL
{
    public class News
    {

        //由预约号得到NewsKey
        public int GetNewKeyByDanNum(string DanNum)
        {
            CIMDataContext db = new CIMDataContext();
            
            int NewsKey = 0;
            try
            {
                NewsKey = db.Dat_News.FirstOrDefault(n => n.DanNum == DanNum).NewsKey;
            }
            catch (Exception ex)
            {

            }
            return NewsKey;
        }

        //添加预约
        public bool AddNews(Dat_News news)
        {
            bool Success ;
            CIMDataContext db = new CIMDataContext();

            db.Dat_News.InsertOnSubmit(news);
            try
            {
                db.SubmitChanges();
                Success = true;
            }
            catch (Exception ex)
            {
                Success = false;
            }
            return Success;
        }

        //删除预约
        public void DelNews(int NewsKey)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_News news = db.Dat_News.Single(n => n.NewsKey == NewsKey);
            db.Dat_News.DeleteOnSubmit(news);
            db.SubmitChanges();
        }

        //任务分配
        public bool EditNews_Shedule(Dat_News news)
        {
            CIMDataContext db = new CIMDataContext();
            try
            {
                Dat_News News = db.Dat_News.Single(n => n.NewsKey == news.NewsKey);
                News.MUserKey = news.MUserKey;
                News.TechOneTime = news.TechOneTime;
                News.TechTwoTime = news.TechTwoTime;
                News.Content_Scheduling = news.Content_Scheduling;
                News.SheduleTime = news.SheduleTime;
                News.SheduleUserKey = news.SheduleUserKey;
                News.UserKey = news.UserKey;
                News.UpdateTime = news.UpdateTime;
                News.Type = news.Type;
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //技术处理
        public bool EditNews_Do(Dat_News news)
        {
            CIMDataContext db = new CIMDataContext();

            Dat_News News = db.Dat_News.Single(n => n.NewsKey == news.NewsKey);

            if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 2 || Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 4)
            {
                switch (news.Type)
                {
                    case 3:
                    case 4:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.Type = news.Type;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        News.HuiDan = news.HuiDan;
                        News.JTfee = news.JTfee;
                        News.ServiceNum = news.ServiceNum;
                        break;
                    case 5:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.RootID = news.RootID;
                        News.Type = news.Type;
                        News.TechFiveTime = news.TechFiveTime;
                        News.TechSixTime = news.TechSixTime;
                        News.Content_Tech_Online = news.Content_Tech_Online;
                        News.Content_Tech = news.Content_Tech;
                        News.Content_Tech_Result = news.Content_Tech_Result;
                        News.FinishTime = news.FinishTime;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        News.HuiDan = news.HuiDan;
                        News.JTfee = news.JTfee;
                        News.ServiceNum = news.ServiceNum;
                        break;
                    case 6:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.Type = news.Type;
                        News.TechFiveTime = news.TechFiveTime;
                        News.TechSixTime = news.TechSixTime;
                        News.Content_Tech_Online = news.Content_Tech_Online;
                        News.Content_Tech = news.Content_Tech;
                        News.Content_Tech_Result = news.Content_Tech_Result;
                        News.FinishTime = news.FinishTime;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        News.HuiDan = news.HuiDan;
                        News.JTfee = news.JTfee;
                        News.ServiceNum = news.ServiceNum;
                        break;
                }
            }
            else    //工程师
            {
                switch (news.Type)
                {
                    case 3:
                    case 4:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.Type = news.Type;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        break;
                    case 5:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.RootID = news.RootID;
                        News.Type = news.Type;
                        News.TechFiveTime = news.TechFiveTime;
                        News.TechSixTime = news.TechSixTime;
                        News.Content_Tech_Online = news.Content_Tech_Online;
                        News.Content_Tech = news.Content_Tech;
                        News.Content_Tech_Result = news.Content_Tech_Result;
                        News.FinishTime = news.FinishTime;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        break;
                    case 6:
                        News.TechOneTime = news.TechOneTime;
                        News.TechTwoTime = news.TechTwoTime;
                        News.Type = news.Type;
                        News.TechFiveTime = news.TechFiveTime;
                        News.TechSixTime = news.TechSixTime;
                        News.Content_Tech_Online = news.Content_Tech_Online;
                        News.Content_Tech = news.Content_Tech;
                        News.Content_Tech_Result = news.Content_Tech_Result;
                        News.FinishTime = news.FinishTime;
                        News.UpdateTime = news.UpdateTime;
                        News.UserKey = news.UserKey;
                        break;
                }
            }
            try
            {
                if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 2 || Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 4)
                {
                    Dat_News news_old = new News().SelectNews(news.NewsKey);

                    //添加考勤记录(只允许当正常->迟到,且选中的任务状态为5、6)

                    if (news_old.HuiDan == "正常" && news.HuiDan == "迟到" && (news.Type == 5 || news.Type == 6))
                    {
                        DateTime StartTime = Convert.ToDateTime(news.TechFiveTime.ToString().Split(' ')[0] + " 09:15:00");

                        //取当天9:15 和  和服务时间上限TechFiveTime比较

                        Dat_KaoQin kaoqin = new Dat_KaoQin
                        {
                            KqType = "迟到",
                            Reason = "迟到",
                            DanNum = news_old.DanNum,
                            StartTime = StartTime,
                            EndTime = Convert.ToDateTime(news.TechFiveTime),
                            SpanTimeD = WebUtility.GetTimeSpanD(StartTime, Convert.ToDateTime(news.TechFiveTime)), 
                            SpanTimeH = WebUtility.GetTimeSpanH(StartTime, Convert.ToDateTime(news.TechFiveTime)),
                            SpanTimeM = WebUtility.GetTimeSpanM(StartTime, Convert.ToDateTime(news.TechFiveTime)),
                            MUserKey = news_old.MUserKey,
                            CreateTime = System.DateTime.Now,
                            UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                        };
                        new KaoQin().AddKaoQin(kaoqin);
                    }

                    //添加交通费(交通费用无变化不添加)
                    if (news_old.JTfee != news.JTfee)
                    {
                        Dat_Fee fee = new Dat_Fee
                        {
                            DanNum = news_old.DanNum,
                            MUserKey = news_old.MUserKey,
                            FeeTime = DateTime.Now,
                            TransFee = news.JTfee,
                            CreateTime = DateTime.Now,
                            UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                        };
                        new Fee().AddFee(fee);
                    }
                }

                db.SubmitChanges();

                //重新生成预约单
                if (News.Type == 5)
                    ReCreateNews(News);

                return true;
            }
            catch (Exception ex)
            {
                WebUtility.Print(ex.Message);
                return false;
            }
        }

        //技术处理-报价
        public bool EditNews_Do_Price(Dat_News news)
        {

            if (news == null)
                return true;

            CIMDataContext db = new CIMDataContext();

            Dat_News News = db.Dat_News.Single(n => n.NewsKey == news.NewsKey);

            News.Quote_StockType = news.Quote_StockType;
            News.Quote_StockID = news.Quote_StockID;
            News.Quote_IsQuote = news.Quote_IsQuote;
            News.Quote_Price = news.Quote_Price;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        //客户评价
        public bool EditNews_Ping(Dat_News news)
        {
            CIMDataContext db = new CIMDataContext();
            try
            {
                Dat_News News = db.Dat_News.Single(n => n.NewsKey == news.NewsKey);
                News.Content_Cus_Ping = news.Content_Cus_Ping;
                News.Content_Cus_PingNum = news.Content_Cus_PingNum;              
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //客户回访
        public bool EditNews_Recall(Dat_News news)
        {
            CIMDataContext db = new CIMDataContext();
            try
            {
                Dat_News News = db.Dat_News.Single(n => n.NewsKey == news.NewsKey);
                News.Content_Cus_Recall = news.Content_Cus_Recall;
                News.Content_Cus_RecallNum = news.Content_Cus_PingNum;
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //重新生成预约单
        private void ReCreateNews(Dat_News news)
        {
            CIMDataContext db = new CIMDataContext();
            Dat_News News = new Dat_News
            {
                DanNum = System.DateTime.Now.ToString("yyyyMMddHHmm"),
                DanNumLID = 0,
                RootID = news.RootID,
                ClsID = news.ClsID,
                UserKey = news.UserKey,
                MUserKey = news.MUserKey,
                Topic = news.Topic,
                Content_Cus = news.Content_Cus,
                CreateTime = System.DateTime.Now,
                UpdateTime = System.DateTime.Now,
                TechOneTime = news.TechOneTime,
                TechTwoTime = news.TechTwoTime,
                UserName = news.UserName,
                UserNameTEL = news.UserNameTEL,
                Type = 1
            };
            db.Dat_News.InsertOnSubmit(News);
            db.SubmitChanges();
        }


        //
        public Dat_News SelectNews(int NewsKey)
        {
            CIMDataContext db = new CIMDataContext();
            try
            {
                return db.Dat_News.Single(n => n.NewsKey == NewsKey);
            }
            catch (Exception ex)
            {
                return new Dat_News();
            }
        }

       

        //某客户单所有单号(带查询分页)
        public IQueryable<ShowNews> SelectNews(Hashtable QueryTable ,int CurPage)
        {   
            int PageSize = Convert.ToInt32(ConfigurationSettings.AppSettings["PageSize"]);

            CIMDataContext db = new CIMDataContext();

            IQueryable<ShowNews> Query;

            if (QueryTable == null)
            {
                Query = from n in db.Dat_News
                                             where 1==1
                                             orderby n.NewsKey descending
                                             select new ShowNews
                                             {
                                                 NewsKey = n.NewsKey,
                                                 DanNum = n.DanNum,
                                                 UserName = n.UserName,
                                                 ClsName = GetClsName(n.ClsID),
                                                 UserKey = n.UserKey,
                                                 Topic = n.Topic,
                                                 Content_Cus = n.Content_Cus,
                                                 TechOneTime = WebUtility.DateTimeToString(n.TechOneTime),
                                                 TechTwoTime = WebUtility.DateTimeToString(n.TechTwoTime),
                                                 ClsID = n.ClsID,
                                                 TypeName = GetTypeName(n.Type),
                                                 MRealName = new Account().GetAccountName(n.MUserKey),
                                                 TechThreeTime = WebUtility.DateTimeToString(n.TechThreeTime),
                                                 TechFourTime = WebUtility.DateTimeToString(n.TechFourTime),
                                                 TechFiveTime = WebUtility.DateTimeToString(n.TechFiveTime),
                                                 TechSixTime = WebUtility.DateTimeToString(n.TechSixTime),
                                                 FinishTime = WebUtility.DateTimeToString(n.FinishTime),
                                                 Content_Cus_PingNum = WebUtility.IntToInt(n.Content_Cus_PingNum),
                                                 Content_Cus_RecallNum = WebUtility.IntToInt(n.Content_Cus_RecallNum),
                                                 UserID = WebUtility.IntToInt(n.UserID),
                                                 UserCompany = n.UserCompany,
                                                 Content_Cus_Ping = n.Content_Cus_Ping,
                                                 Type = n.Type,
                                                 UpdateTime = WebUtility.DateTimeToString(n.UpdateTime),
                                                 HuiDan = string.IsNullOrEmpty(n.HuiDan) ? "正常" : n.HuiDan,
                                                 JTfee = WebUtility.DecimalToDecimal(n.JTfee),
                                                 Fetch_FetchType = n.Fetch_FetchType,
                                                 Fetch_FetchID = WebUtility.IntToInt(n.Fetch_FetchID),
                                                 Fetch_DanNum = n.Fetch_DanNum,
                                                 Quote_StockType = n.Quote_StockType,
                                                 Quote_StockID = WebUtility.IntToInt(n.Quote_StockID),
                                                 Quote_IsQuote = n.Quote_IsQuote,
                                                 Quote_Price = WebUtility.DecimalToDecimal(n.Quote_Price)
                                             };
                return Query.Skip((CurPage - 1) * PageSize).Take(PageSize);
            }
            else
            {
                Query = from n in GetConditionQuery(QueryTable)
                                              orderby n.NewsKey descending
                                              select new ShowNews
                                              {
                                                  NewsKey = n.NewsKey,
                                                  DanNum = n.DanNum,
                                                  UserName = n.UserName,
                                                  ClsName = GetClsName(n.ClsID),
                                                  UserKey = n.UserKey,
                                                  Topic = n.Topic,
                                                  Content_Cus = n.Content_Cus,
                                                  TechOneTime = WebUtility.DateTimeToString(n.TechOneTime),
                                                  TechTwoTime = WebUtility.DateTimeToString(n.TechTwoTime),
                                                  ClsID = n.ClsID,
                                                  TypeName = GetTypeName(n.Type),
                                                  MRealName = new Account().GetAccountName(n.MUserKey),
                                                  TechThreeTime = WebUtility.DateTimeToString(n.TechThreeTime),
                                                  TechFourTime = WebUtility.DateTimeToString(n.TechFourTime),
                                                  TechFiveTime = WebUtility.DateTimeToString(n.TechFiveTime),
                                                  TechSixTime = WebUtility.DateTimeToString(n.TechSixTime),
                                                  FinishTime = WebUtility.DateTimeToString(n.FinishTime),
                                                  Content_Cus_PingNum = WebUtility.IntToInt(n.Content_Cus_PingNum),
                                                  Content_Cus_RecallNum = WebUtility.IntToInt(n.Content_Cus_RecallNum),
                                                  UserID = WebUtility.IntToInt(n.UserID),
                                                  UserCompany = n.UserCompany,
                                                  Content_Cus_Ping = n.Content_Cus_Ping,
                                                  Type = n.Type,
                                                  UpdateTime = WebUtility.DateTimeToString(n.UpdateTime),
                                                  HuiDan = string.IsNullOrEmpty(n.HuiDan) ? "正常" : n.HuiDan,
                                                  JTfee = WebUtility.DecimalToDecimal(n.JTfee),
                                                  Fetch_FetchType = n.Fetch_FetchType,
                                                  Fetch_FetchID = WebUtility.IntToInt(n.Fetch_FetchID),
                                                  Fetch_DanNum = n.Fetch_DanNum,
                                                  Quote_StockType = n.Quote_StockType,
                                                  Quote_StockID = WebUtility.IntToInt(n.Quote_StockID),
                                                  Quote_IsQuote = n.Quote_IsQuote,
                                                  Quote_Price = WebUtility.DecimalToDecimal(n.Quote_Price)
                                              };
                return Query.Skip((CurPage - 1) * PageSize).Take(PageSize); 
            }
        }

        //某客户单所有单号(带查询)
        public int SelectNewsCount(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();

            int count;
            if (QueryTable == null)
                count = db.Dat_News.Count();
            else
            {
                count = GetConditionQuery(QueryTable).Count();
            }

            return count;
        }


        private IQueryable<Dat_News> GetConditionQuery(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();
            
            var predicate = PredicateBuilder.True<Dat_News>();

            if (QueryTable["Query"] != null)
            {
                switch (QueryTable["Query"].ToString())
                {
                    case "all":
                        break;
                    case "day":
                        predicate = predicate.And(n => Convert.ToDateTime(n.TechOneTime).Date == System.DateTime.Now.Date);
                        break;
                    case "month":
                        predicate = predicate.And(n => Convert.ToDateTime(n.TechOneTime).Year == System.DateTime.Now.Year);
                        predicate = predicate.And(n => Convert.ToDateTime(n.TechOneTime).Month == System.DateTime.Now.Month);
                        break;
                }
            }

            if (QueryTable["Type"] != null)
            {
                predicate = predicate.And(n => QueryTable["Type"].ToString().IndexOf(n.Type.ToString()) >= 0);
            }
            if (QueryTable["ClsID"] != null)
                predicate = predicate.And(n => n.ClsID.Contains(QueryTable["ClsID"].ToString()));
            if (QueryTable["txt_Type"] != null)
                predicate = predicate.And(n => n.Type == Convert.ToInt32(QueryTable["txt_Type"]));
            if (QueryTable["Keyword"] != null)
                predicate = predicate.And(n => n.Topic.Contains(QueryTable["Keyword"].ToString().Trim()) || n.DanNum.Contains(QueryTable["Keyword"].ToString().Trim()) || n.Content_Cus.Contains(QueryTable["Keyword"].ToString().Trim()));
            if (QueryTable["OneDate"] != null)
            {
                try
                {
                    DateTime OneDate = Convert.ToDateTime(QueryTable["OneDate"]);
                    predicate = predicate.And(n => n.TechOneTime >= OneDate);
                }
                catch (Exception ex) { }
            }
            if (QueryTable["TwoDate"] != null)
            {
                try
                {
                    DateTime TwoDate = Convert.ToDateTime(QueryTable["TwoDate"].ToString().Split(' ')[0] + " 23:59:59");
                    predicate = predicate.And(n => n.TechTwoTime <= TwoDate);
                }
                catch (Exception ex) { }
            }

            if (QueryTable["Type"] != null)
            {
                predicate = predicate.And(n => QueryTable["Type"].ToString().IndexOf(n.Type.ToString()) >= 0);
            }
            if (QueryTable["ClsIDType"] != null)
            {
                predicate = predicate.And(n => n.ClsID.Contains(QueryTable["ClsIDType"].ToString()));
            }
            if (QueryTable["MenuType"] != null)
            {
                int UserType = Convert.ToInt32(HttpContext.Current.Session["UserType"]);
                int UserKey = Convert.ToInt32(HttpContext.Current.Session["UserKey"]);

                if (UserType == 1)  //客户显示自己提交的记录  MenuType=112,113
                {
                    predicate = predicate.And(n => n.UserID == UserKey);
                }

                if (UserType == 3)  //工程师显示被指定的记录
                {
                    predicate = predicate.And(n => n.MUserKey == UserKey);
                }


                //客户回访
                if (Convert.ToInt32(QueryTable["MenuType"]) == 212)
                {
                    predicate = predicate.And(n => n.Content_Cus_PingNum > 0);
                }
            }

            return db.Dat_News.Where(predicate);
        }

        //获得流水号
        public string GetMaxOrderID()
        {
            CIMDataContext db = new CIMDataContext();

            int MaxID = 1;

            var Query = (from n in db.Dat_News
                         where n.CreateTime.ToString().Contains(System.DateTime.Now.ToShortDateString())
                         orderby n.DanNumLID descending
                         select n).Take(1);

            if (Query.Count() > 0)
            {
                MaxID = Convert.ToInt32(Query.First().DanNumLID) + 1;
            }

            string StrMaxID = MaxID.ToString();

            if (StrMaxID.Length < 3)
            {
                int Length = StrMaxID.Length;
                for (int i = 0; i < 3 - Length; i++)
                {
                    StrMaxID = "0" + StrMaxID;
                }
            }

            return StrMaxID;

        }

        //-------------------------------任务单列表-相关显示转换-----------------


        //服务类转换
        public string GetClsName(string ClsID)
        {
            string val="";

            using (CIMEntities en = new CIMEntities())
            {
                string sql = " select value it from CIMEntities.NewsTypeSet as it  where it.TypeKey in {" + ClsID + "}";
                IQueryable<NewsType> newstype = en.CreateQuery<NewsType>(sql);

                foreach (var t in newstype)
                    val += t.TypeName + "、";
                if (val.Length > 0) val = val.Substring(0, val.Length - 1);
            }
            return val;
        }

        //服务类转换间隔符带颜色
        public static string GetClsNameWithColor(object ClsID)
        {
            return ClsID.ToString().Replace(",", "、");
        }

        //计数器
        public static int GetCountByType(string TypeList,string TimeType)
        {
            CIMDataContext db = new CIMDataContext();

            if (TypeList != "")
            {
                string[] TypeList_Arr = TypeList.Trim().Split(',');

                if (TimeType == "A")
                {

                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 4 || Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 2)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 
                                select n).Count();
                    }

                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 3)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 &&
                                n.MUserKey == Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                                select n).Count();
                    }
                }

                if (TimeType == "D")
                {

                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 4 || Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 2)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 &&
                                System.DateTime.Now.Date == Convert.ToDateTime(n.TechOneTime).Date
                                select n).Count();
                    }

                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 3)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 &&
                                System.DateTime.Now.Date == Convert.ToDateTime(n.TechOneTime).Date &&
                                n.MUserKey == Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                                select n).Count();
                    }
                }

                if (TimeType == "M")
                {
                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 4 || Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 2)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 &&
                                System.DateTime.Now.Year == Convert.ToDateTime(n.TechOneTime).Year &&
                                System.DateTime.Now.Month == Convert.ToDateTime(n.TechOneTime).Month 
                                select n).Count();
                    }

                    if (Convert.ToInt32(HttpContext.Current.Session["UserType"]) == 3)
                    {
                        return (from n in db.Dat_News
                                where TypeList.IndexOf(n.Type.ToString()) >= 0 &&
                                System.DateTime.Now.Year == Convert.ToDateTime(n.TechOneTime).Year &&
                                System.DateTime.Now.Month == Convert.ToDateTime(n.TechOneTime).Month &&
                                n.MUserKey == Convert.ToInt32(HttpContext.Current.Session["UserKey"])
                                select n).Count();
                    }
                }

                return 0;
            }
            else
                return 0;
        }

        //服务状态
        public string GetTypeName(int Type)
        {
            string val = "";

            switch (Type)
            {
                case 1:
                    val = "任务已提交";
                    break ;
                case 2:
                    val = "任务已分配";
                    break ;
                case 3:
                    val = "技术员途中";
                    break;
                case 4:
                    val = "任务处理中";
                    break;
                case 5:
                    val = "任务未完成";
                    break;
                case 6:
                    val = "任务已完成";
                    break;
                default:
                    val = "";
                    break;
            }
            return val;
        }

        //送取服务内容
        public static string GetFetchContent(string FetchType, int FetchID)
        {
            if (FetchType == "发票")
                return new Invoice().SelectInvoice(FetchID).InvoiceNO;
            if (FetchType == "报修出库物品")
                return new WxStock().SelectWxStock(FetchID).StockName;
            return "";
        }

        //报价服务内容
        public static string GetQuoteContent(string StockType, int StockID)
        {
            if (StockType == "库存物品")
                return new Stock().SelectStock(StockID).StName;
            else if (StockType == "报修入库物品")
                return new WxStock().SelectWxStock(StockID).StockName;
            else
                return "";
        }

        public static string GetQuoteContentLink(string StockType, int StockID)
        {
            string a_href = "<a class='thickbox' href={0}?{1}={2}&KeepThis=true&TB_iframe=true&height=350&width=650'>{3}</a>";

            if (StockType == "库存物品")
                return String.Format(a_href, "../Stock/Stock_View.aspx", "stockkey", StockID.ToString(), new Stock().SelectStock(StockID).StName);
            else if (StockType == "报修入库物品")
                return String.Format(a_href, "../Stock/WxStock_View.aspx", "wxstockkey", StockID.ToString(), new WxStock().SelectWxStock(StockID).StockName);
            else
                return "";
        }

        //是否报价判断
        public static bool IsQuote(object Quote_IsQuote)
        {
            if ( Quote_IsQuote == null)
                return false;
            else if ( Quote_IsQuote.ToString()=="未报价" )
                return false;
            else
               return true;
        }

        //由任务单号取得客户简称
        public static string GetCusName(object DanNum)
        {
            if (DanNum == null)
                return "";
            else
            {
                int NewsKey = new News().GetNewKeyByDanNum(DanNum.ToString());
                if (NewsKey == 0)
                    return "";
                else
                    try
                    {
                        int UserID = (new News().SelectNews(NewsKey).UserID.HasValue) ? Convert.ToInt32(new News().SelectNews(NewsKey).UserID) : 0;
                        return new Account().GetAccountShortAllName(UserID);
                    }
                    catch (Exception ex)
                    {
                        return "";
                    }
            }
        }

        //-------------------------------添加任务单-任务类型选取-----------------

        //复选框字符串
        public static string GetChkListValue(CheckBoxList chk)
        {
            string val = "";
            for (int i=0;i<chk.Items.Count;i++)
            {
                if (chk.Items[i].Selected)
                    val += chk.Items[i].Value + ",";
            }
            return  val.Length>0?(val.Substring(0,val.Length-1)):"";
        }

        //复选框字符串全不选
        public static void ChkListClear(CheckBoxList chk)
        {
            for (int i = 0; i < chk.Items.Count; i++)
            {
                if (chk.Items[i].Selected)
                    chk.Items[i].Selected = false;
            }
        }

        //------------------------------

        public void ResetJTfee(string DanNum)
        {
            CIMDataContext db = new CIMDataContext();
            int  NewsKey = GetNewKeyByDanNum(DanNum);

            if (NewsKey != 0)
            {
                Dat_News news = db.Dat_News.SingleOrDefault( n => n.NewsKey == NewsKey );
                news.JTfee = null;
                db.SubmitChanges();
            }
        }

        public void ResetKaoQin(string DanNum)
        {
            CIMDataContext db = new CIMDataContext();
            int NewsKey = GetNewKeyByDanNum(DanNum);

            if (NewsKey != 0)
            {
                Dat_News news = db.Dat_News.SingleOrDefault(n => n.NewsKey == NewsKey);
                news.HuiDan = "正常";
                db.SubmitChanges();
            }
        }
    }

        

    public class ShowNews
    {   
        private int newskey;
        public int NewsKey
        {
            get { return newskey; }
            set { newskey = value; }
        }

        private string dannum;
        public string DanNum
        {
            get { return dannum; }
            set { dannum = value; }
        }

        private string username;
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }

        private string clsname;
        public string ClsName
        {
            get { return clsname; }
            set { clsname = value; }
        }

        private int userkey;
        public int UserKey
        {
            get { return userkey; }
            set { userkey = value; }
        }

        private string topic;
        public string Topic
        {
            get { return topic; }
            set { topic = value; }
        }

        private string content_cus;
        public string Content_Cus
        {
            get { return content_cus; }
            set { content_cus = value; }
        }

        private string techonetime;
        public string TechOneTime
        {
            get { return techonetime; }
            set { techonetime = value; }
        }

        private string techtwotime;
        public string TechTwoTime
        {
            get { return techtwotime; }
            set { techtwotime = value; }
        }

        private string techfivetime;
        public string TechFiveTime
        {
            get { return techfivetime; }
            set { techfivetime = value; }
        }

        private string techsixtime;
        public string TechSixTime
        {
            get { return techsixtime; }
            set { techsixtime = value; }
        }

        private string clsid;
        public  string ClsID
        {
            get { return clsid; }
            set { clsid = value; }
        }

        private string typename;
        public string TypeName
        {
            get { return typename; }
            set { typename = value; }
        }

        private string mrealname;
        public string MRealName
        {
            get { return mrealname; }
            set { mrealname = value; }
        }

        private string techthreetime;
        public string TechThreeTime
        {
            get { return techthreetime; }
            set { techthreetime = value; }
        }

        private string techfourtime;
        public string TechFourTime
        {
            get { return techfourtime; }
            set { techfourtime = value; }
        }

        private string finishtime;
        public string FinishTime
        {
            get { return finishtime; }
            set { finishtime = value; }
        }

        private int content_cus_pingnum;
        public int Content_Cus_PingNum
        {
            get { return content_cus_pingnum; }
            set { content_cus_pingnum = value; }
        }

        private int content_cus_recallnum;
        public int Content_Cus_RecallNum
        {
            get { return content_cus_recallnum; }
            set { content_cus_recallnum = value; }
        }

        private int userid;
        public int UserID
        {
            get { return userid; }
            set { userid = value; }
        }

        private string usercompany;
        public string UserCompany
        {
            get { return usercompany; }
            set { usercompany = value; }
        }

        private string content_cus_ping;
        public string Content_Cus_Ping
        {
            get { return content_cus_ping; }
            set { content_cus_ping = value; }
        }

        private int type;
        public int Type
        {
            get { return type; }
            set { type = value; }
        }

        private string updatetime;
        public string UpdateTime
        {
            get { return updatetime; }
            set { updatetime = value; }
        }

        private string huidan;
        public string HuiDan
        {
            get { return huidan; }
            set { huidan = value; }
        }

        private decimal jtfee;
        public decimal JTfee
        {
            get { return jtfee; }
            set { jtfee = value; }
        }

        private string fetch_fetchtype;
        public string Fetch_FetchType
        {
            get { return fetch_fetchtype; }
            set { fetch_fetchtype = value; }
        }

        private int fetch_fetchid;
        public int Fetch_FetchID
        {
            get { return fetch_fetchid; }
            set { fetch_fetchid = value; }
        }

        private string fetch_dannum;
        public string Fetch_DanNum
        {
            get { return fetch_dannum; }
            set { fetch_dannum = value; }
        }

        private string quote_stocktype;
        public string Quote_StockType
        {
            get { return quote_stocktype; }
            set { quote_stocktype = value; }
        }

        private int quote_stockid;
        public int Quote_StockID
        {
            get { return quote_stockid; }
            set { quote_stockid = value; }
        }

        private string quote_isquote;
        public string Quote_IsQuote
        {
            get { return quote_isquote; }
            set { quote_isquote = value; }
        }

        private decimal quote_price;
        public decimal Quote_Price
        {
            get { return quote_price; }
            set { quote_price = value; }
        }
    }
}
